package com.share.lottery.activity.loginandre;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.share.lottery.R;
import com.share.lottery.activity.JcMainActivtiy;
import com.share.lottery.base.BaseActivity;
import com.share.lottery.base.Constants;
import com.share.lottery.http.UsesrController;
import com.share.lottery.model.User;
import com.share.lottery.utils.AppUtils;
import com.share.lottery.utils.StringUtil;

/**
 * @author py
 *登录
 */
public class LoginActivity extends BaseActivity{
	private EditText mEtUserName;
	private EditText mEtPwd;
	private Button mBtnLogin;
	private Button mBtnRegist;
	private String userName;

	private String passWord;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		if (isLogin()) {

			turnToActivity(JcMainActivtiy.class, true);
			return;
		}
		init();
	}
	private boolean isLogin(){
		User user = AppUtils.getUserBean();
		if (user != null) {
			return true;
		}else {
			return false;
		}

	}
	/**
	 * 初始化头信息
	 */
	private TextView tvTitleCenter;
	private ImageButton imgLeft;
	private ImageButton imgRight;
	private void intTitle() {
		tvTitleCenter = (TextView) findViewById(R.id.title_tv_center);
		imgLeft = (ImageButton) findViewById(R.id.title_left_img);
		imgRight = (ImageButton) findViewById(R.id.title_right_img);
		imgLeft.setVisibility(View.VISIBLE);
		imgLeft.setOnClickListener(new OnClickListener() {


			public void onClick(View v) {
				LoginActivity.this.finish();

			}
		});
		tvTitleCenter.setText("登录");

	}
	public void findView() {
		intTitle();
		mEtUserName = (EditText) findViewById(R.id.et_user);
		mEtPwd = (EditText) findViewById(R.id.password);
		mBtnLogin = (Button) findViewById(R.id.login);
		mBtnLogin.setOnClickListener(this);
		mBtnRegist = (Button) findViewById(R.id.regist);
		mBtnRegist.setOnClickListener(this);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login://登录
			userName = mEtUserName.getText().toString();
			passWord = mEtPwd.getText().toString();
			if (StringUtil.isNullOrEmpty(userName)) {
				showLongToast("用户名不能为空");
				return;
			}	
			if (StringUtil.isNullOrEmpty(passWord)) {
				showLongToast("密码不能为空");
				return;
			}
			showProgreessDialog();

			loginForServer(userName,passWord);//自己的服务器

			break;
		case R.id.regist://跳转到注册页面
			turnToActivity(RegisterActivity.class, false);
			break;

		default:
			break;
		}
	}


	private void loginForServer(String userName2, String passWord2) {
		showProgreessDialog();
		UsesrController.getInstance().login(mHandler, userName2, passWord2);
	}

	private void doMessage(Message msg,int tag) {
		if (tag == 0) {


			switch (msg.arg1) {
			case Constants.HTTP_OK_LASTID://返回laseid


				break;

			case Constants.HTTP_OK://请求成功
				
				String token = (String) msg.obj;
				if (!StringUtil.isNullOrEmpty(token)) {
					showShortToast("用户验证成功");
					UsesrController.getInstance().getUserInfo(mHandler, token);
					
				}else {
					showShortToast("用户验证失败");
				}
				break;
			case Constants.HTTP_ERROR://请求错误
				String serror = (String) msg.obj;
				showShortToast("用户名或密码错误");

				break;
			case Constants.HTTP_ERROR_PASER://数据解析错误
				showShortToast("数据解析错误");
				break;
			case Constants.HTTP_ERROR_NET://网络错误

			default:
				break;
			}	
		}else if(tag == 1){
			switch (msg.arg1) {
			case Constants.HTTP_OK_LASTID://返回laseid


				break;

			case Constants.HTTP_OK://请求成功
				User user = (User) msg.obj;
				if (user == null) {
					showShortToast("获取用户信息失败");
				}else {
					showShortToast("获取用户信息成功");
					AppUtils.saveUserBean(user);
				}
				
			
				break;
			case Constants.HTTP_ERROR://请求错误
				String serror = (String) msg.obj;
				showShortToast("用户名或密码错误");

				break;
			case Constants.HTTP_ERROR_PASER://数据解析错误
				showShortToast("数据解析错误");
				break;
			case Constants.HTTP_ERROR_NET://网络错误

			default:
				break;
			}
		}

	}

	Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			dismissProgressDialog();

			switch (msg.what) {
			case Constants.login:
				doMessage(msg,0);
				break;
			case Constants.loginToken:
				doMessage(msg,1);
				break;

			default:
				break;
			}

		}


	};

	public void getData() {


	}


	public void initTitle() {


	}


	public void setData() {


	}


	public void setonCliker() {


	}



}
